python vars keyword

33

- If an object has an attribute __dict__, vars() will return it. 
- A typycal example is vars() applied after parsing arguments with the 'argparse' module, 
  which allows you to access the parsed arguments as key - value of a dict
  
import argparse
parser = argparse.ArgumentParser(description = "Your description", epilog = "Bye!")
parser.add_argument("A", type = str, help = "Mandatory argument A")
parser.add_argument("B", type = str, help = "Mandatory argument B")
parser.add_argument("--C", type = int, default = 5, help = "Optional argument C")
args = vars(parser.parse_args)
a = args["A"]
b = args["B"]
c = args["C"]

Comments

Submit
0 Comments